package com.itboye.hutouben.activity.store;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itboye.hutouben.R;
import com.itboye.hutouben.activity.store.fragment.OrderFragment;
import com.itboye.hutouben.adapter.OrderViewPagerAdapter;
import com.itboye.hutouben.base.BaseActivity;

import java.util.ArrayList;

/**
 * 我的订单
 */
public class OrderActivity extends BaseActivity {
    private TextView view0,view1, view2, view3, view4, add_shap_title_tv;
    private ViewPager mPager;
    private ImageView image;
    private int currIndex;// 当前页卡编号
    private float offset;// 图片移动的偏移量
    private ArrayList<Fragment> fragmentList;
    //返回箭头
    private ImageView close_icon;
    public static OrderActivity act;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order);
        add_shap_title_tv.setText(getString(R.string.myorder));
        act=this;
        initTextView();
        initImage();
        initViewPager();
    }
    public void initViewPager02(int type) {
        mPager = (ViewPager) findViewById(R.id.viewpager);
        fragmentList = new ArrayList<>();

        fragmentList.add(new OrderFragment(0));
        fragmentList.add(new OrderFragment(1));
        fragmentList.add(new OrderFragment(2));
        fragmentList.add(new OrderFragment(3));
        fragmentList.add(new OrderFragment(4));
        //
        // //给ViewPager设置适配器
        mPager.setAdapter(new OrderViewPagerAdapter(
                getSupportFragmentManager(), fragmentList));
        mPager.setCurrentItem(type);// 设置当前显示标签页为第一页
    }

    /*
     * 初始化ViewPager
	 */
    public void initViewPager() {
        mPager = (ViewPager) findViewById(R.id.viewpager);
        fragmentList = new ArrayList<>();
        Fragment quanbuFragment = new OrderFragment(0);
        Fragment daifukuanFragment = new OrderFragment(1);
        Fragment daifahuoFragment = new OrderFragment(2);
        Fragment daishouhuoFragment = new OrderFragment(3);
        Fragment yiwanchengFragment = new OrderFragment(4);
        fragmentList.add(quanbuFragment);
        fragmentList.add(daifukuanFragment);
        fragmentList.add(daifahuoFragment);
        fragmentList.add(daishouhuoFragment);
        fragmentList.add(yiwanchengFragment);

        Intent intent = getIntent();
        int i = intent.getIntExtra("index", 0);
        currIndex = i;
        //
        // //给ViewPager设置适配器
        mPager.setAdapter(new OrderViewPagerAdapter(
                getSupportFragmentManager(), fragmentList));

        mPager.setCurrentItem(i);// 设置当前显示标签页为第一页
        setTextColor(i + 1);

        tranSlate(i, 0);

        mPager.setOnPageChangeListener(new MyOnPageChangeListener());// 页面变化时的监听器

    }

    public class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {
        private float one = offset;// 两个相邻页面的偏移量

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageSelected(int arg0) {
            // TODO Auto-generated method stub

            // Toast.makeText(App.ctx, ""+arg0, 0).show();
            // Animation animation = new TranslateAnimation(currIndex * one,
            // arg0
            // * one, 0, 0);// 平移动画
            // currIndex = arg0;
            // animation.setFillAfter(true);// 动画终止时停留在最后一帧，不然会回到没有执行前的状态
            // animation.setDuration(200);// 动画持续时间0.2秒
            // image.startAnimation(animation);// 是用ImageView来显示动画的

            tranSlate(arg0, 200);
            currIndex = arg0;
            int i = currIndex + 1;
            setTextColor(i);

            // Toast.makeText(MyOrderActivity.this, "您选择了第"+i+"个页卡",
            // Toast.LENGTH_SHORT).show();
        }
    }

    // 平移动画
    public void tranSlate(int currentItem, long durationMillis) {
        float one = offset;
        Log.v("offest", "" + offset);
        Animation animation = new TranslateAnimation(currIndex * one,
                currentItem * one, 0, 0);// 平移动画
        Log.v("currentoffest", "" + currIndex * one);
        Log.v("currentItem", "" + currentItem * one);
        animation.setFillAfter(true);// 动画终止时停留在最后一帧，不然会回到没有执行前的状态
        animation.setDuration(durationMillis);// 动画持续时间0.2秒
        image.startAnimation(animation);// 是用ImageView来显示动画的
    }

    // 设置顶部条目颜色的变化
    public void setTextColor(int i) {
        switch (i) {
            case 1:
                view0.setTextColor(getResources().getColor(R.color.main_color));
                view1.setTextColor(getResources().getColor(R.color.black));
                view2.setTextColor(getResources().getColor(R.color.black));
                view3.setTextColor(getResources().getColor(R.color.black));
                view4.setTextColor(getResources().getColor(R.color.black));
                break;
            case 2:
                view0.setTextColor(getResources().getColor(R.color.black));
                view1.setTextColor(getResources().getColor(R.color.main_color));
                view2.setTextColor(getResources().getColor(R.color.black));
                view3.setTextColor(getResources().getColor(R.color.black));
                view4.setTextColor(getResources().getColor(R.color.black));
                break;
            case 3:
                view0.setTextColor(getResources().getColor(R.color.black));
                view1.setTextColor(getResources().getColor(R.color.black));
                view2.setTextColor(getResources().getColor(R.color.main_color));
                view3.setTextColor(getResources().getColor(R.color.black));
                view4.setTextColor(getResources().getColor(R.color.black));
                break;
            case 4:
                view0.setTextColor(getResources().getColor(R.color.black));
                view1.setTextColor(getResources().getColor(R.color.black));
                view2.setTextColor(getResources().getColor(R.color.black));
                view3.setTextColor(getResources().getColor(R.color.main_color));
                view4.setTextColor(getResources().getColor(R.color.black));
                break;
            case 5:
                view0.setTextColor(getResources().getColor(R.color.black));
                view1.setTextColor(getResources().getColor(R.color.black));
                view2.setTextColor(getResources().getColor(R.color.black));
                view3.setTextColor(getResources().getColor(R.color.black));
                view4.setTextColor(getResources().getColor(R.color.main_color));
                break;
            default:
                break;
        }
    }

    /*
	 * 初始化图片的位移像素
	 */
    public void initImage() {
        image = (ImageView) findViewById(R.id.cursor);
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        int screenW = dm.widthPixels;
        int screenH = dm.heightPixels;
        offset = screenW / 5.0f;
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                (int) offset - 20, 2);
        image.setLayoutParams(params);
    }


    // 初始化标签
    public void initTextView() {
        view0 = (TextView) findViewById(R.id.tv_guid0);
        view1 = (TextView) findViewById(R.id.tv_guid1);
        view2 = (TextView) findViewById(R.id.tv_guid2);
        view3 = (TextView) findViewById(R.id.tv_guid3);
        view4 = (TextView) findViewById(R.id.tv_guid4);
        view0.setOnClickListener(new txListener(0));
        view1.setOnClickListener(new txListener(1));
        view2.setOnClickListener(new txListener(2));
        view3.setOnClickListener(new txListener(3));
        view4.setOnClickListener(new txListener(4));
    }

    public class txListener implements View.OnClickListener {
        private int index = 0;

        public txListener(int i) {
            index = i;
        }

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            mPager.setCurrentItem(index);
        }
    }


    @Override
    protected void onInit() {

    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.close_icon:
                finish();
                break;

        }
    }
}
